package com.zz.framework.core.ext

import android.widget.LinearLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.divider.MaterialDividerItemDecoration
import com.zz.framework.R
import com.zz.framework.widget.recyclerviewflexibledivider.HorizontalDividerItemDecoration
import com.zz.framework.widget.recyclerviewflexibledivider.VerticalDividerItemDecoration

/**
 * Copyright © 2025 zhun All rights reserved.
 * Created by ZhaoZhun on 2025/2/22 20:03.
 * @author: ZhaoZhun 1820022519@qq.com
 * @version: V1.0
 */
fun RecyclerView.addHorizontalDividerItemDecoration(size: Int = 1, colorResId: Int = R.color.color_divider, isShowLastDivider: Boolean = true) {
    addItemDecoration(
        HorizontalDividerItemDecoration.Builder(context)
            .colorResId(colorResId).size(size).also {
                if (isShowLastDivider) {
                    it.showLastDivider()
                }
            }
            .build()
    )
}

fun RecyclerView.addVerticalDividerItemDecoration(size: Int = 1, colorResId: Int = R.color.color_divider, isShowLastDivider: Boolean = true) {
    addItemDecoration(
        VerticalDividerItemDecoration.Builder(context)
            .colorResId(colorResId).size(size).also {
                if (isShowLastDivider) {
                    it.showLastDivider()
                }
            }
            .build()
    )
}

fun RecyclerView.scrollToPositionWithOffset(positionToScroll: Int, offset: Int = 0) {
    val layoutManager = layoutManager as LinearLayoutManager
    postDelayed({
        layoutManager.scrollToPositionWithOffset(positionToScroll, offset)
    }, 10)
}